package cn.signalpha.common.config;

import cn.hutool.core.io.FileUtil;
import cn.signalpha.core.swagger.EnableSwagger;
import lombok.AllArgsConstructor;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

@EnableSwagger
@AllArgsConstructor
@Configuration(proxyBeanMethods = false)
public class UploadFileConfig {

	private static final String TMP_UPLOAD_LOCATION = "/tmp/mtr_file";

	@Bean
	public MultipartConfigElement multipartConfigElement() {
		// 创建存储的文件夹 避免使用的时候找不到
		// 这里用的是 Hutool 的工具类,可以自己写
		FileUtil.mkdir(TMP_UPLOAD_LOCATION);
		MultipartConfigFactory factory = new MultipartConfigFactory();
		factory.setLocation(TMP_UPLOAD_LOCATION);
		// 单个文件最大M
		factory.setMaxFileSize(DataSize.ofMegabytes(1024));
		// 设置总上传数据总大小 M
		factory.setMaxRequestSize(DataSize.ofMegabytes(2048));
		return factory.createMultipartConfig();
	}
}
